草庐IT

C++ operator+ 和 operator+= 重载

全部标签

c++ - 对于派生类对象的基类部分,如何在基类中实现运算符重载?

对于派生类对象的基类部分,如何在基类中实现运算符重载?请查看此示例代码并实现基类部分以在派生类对象上实现*运算符classbase{intx;public:};classder:publicbase{inty;public:constderoperator*(constder&rh){derd;d.y=y*rh.y;returnd;}}; 最佳答案 classbase{intx;public:base&operator*=(constbase&rh){x*=rh.x;return*this;}baseoperator*(constb

c++ - 与运算符重载不匹配运算符+错误

我正在学习gameinsitute的C++编程类(class),其中有一个运算符重载的示例,我不断得到一个main.cpp|20|error:nomatchfor‘operator+’in‘v+w’我不知道问题出在哪里。主要.cpp//main.cpp#include"Vector3.h"#includeusingnamespacestd;intmain(){floatcoords[3]={1.0f,2.0f,3.0f};Vector3u;Vector3v(coords);Vector3w(-5.0f,2.0f,0.0f);coutVector3.h#ifndefVECTOR3_H#d

c++ - 为什么 uBLAS 没有 `operator*(matrix, vector)` ?

在doc,他们说Wedecidedtousenooperatoroverloadingfor...他们为这些提供了prod。但为什么?有什么好的理由吗?我喜欢做matrix*vector(和大多数其他语言一样)。我想了解为什么他们没有重载此运算符以了解为什么自己做可能是个坏主意。或者,如果我自己重载,它们不会有任何缺点吗? 最佳答案 可能是因为op*在其他语言中,例如使用Python中的Numpy,将始终是元素明智的。如果一个元素是矩阵而另一个元素是vector,它将尝试广播缺失维度中的所有元素。

c++ - std::map 是否要求比较器的 operator() 为常量?

在OSX10.8上使用libc++时,以下代码无法使用XCode4.5的clang++进行编译:#include#includeclassFoo{public:explicitFoo(intval_):val(val_){}intval;};structFooComparator{booloperator()(constFoo&left,constFoo&right){returnleft.valm;Foof(4);m[f]=std::string("four");return0;}错误:broken.cpp:11:8:note:candidatefunctionnotviable:'

c++ - 在存在新的初始化序列的情况下,运算符重载决议如何工作?

给定以下代码:#include#includetemplateclassConvertProxy{Sourceconst*m_source;public:ConvertProxy(Sourceconst&source):m_source(&source){}templateoperatorDest()const{returnDest(m_source->begin(),m_source->end());}};templateConvertProxyconvert(Sourceconst&source){returnConvertProxy(source);}intmain(){std:

c++ - 模板成员函数重载问题

N4296::13.1/2.2[over.load]的标准说:Likewise,memberfunctiontemplatedeclarationswiththesamename,thesameparameter-type-list,andthesametemplateparameterlistscannotbeoverloadedifanyofthemisastaticmemberfunctiontemplatedeclaration.所以,我认为下面的程序是病式的:#includestructA{templatestaticvoidfoo(){}templatestaticintf

c++ - 为什么不使用强制转换语法调用 "operator void"?

在玩thisanswer时通过userGMan我制作了以下代码片段(使用VisualC++9编译):classClass{public:operatorvoid(){}};Classobject;static_cast(object);(void)object;object.operatorvoid();通过调试器后,我发现转换为void不会调用Class::operatorvoid(),只有第三次调用(显式调用运算符)实际上调用了运算符,这两个转换什么都不做。为什么operatorvoid没有用强制转换语法调用? 最佳答案 在§1

c++ - 保留基本功能的重载赋值运算符

是否可以在不完全重新实现的情况下重载=运算符?我想为它指定特殊的行为-如果输入对象有一些特殊的值->运算符应该做一些额外的工作。如果不是-它应该作为基本赋值运算符。有点像operator=(input)if(input==specialValue)setParam(this->true)base() 最佳答案 您可以使用ifelse语句来实现,在else部分指定基本功能,在if或elseif部分指定您的条件,如果条件为真,则根据该值执行您的操作返回一些特定值。operator=(input)if(input==specialValu

c++ - 基于非类型模板参数的重载

我们熟悉基于函数参数的重载。但是为什么我们不能基于非类型模板参数进行重载呢?有了这样的重载,您就不必为了重载目的而添加额外的函数参数,这可能会对运行时性能产生负面影响。唉,下面的代码无法编译:templatevoidfunc(){}templatevoidfunc(){}intmain(){func();}产生的错误信息是error:callofoverloaded'func()'isambiguousfunc();^note:candidate:voidfunc()[withbool=false]voidfunc(){}^note:candidate:voidfunc()[withi

c++ - 类模板多重继承和函数重载

为什么下面main中对waitForEvent的函数调用不明确?#includestructEvent1{charc1[1];};structEvent2{charc2[2];};templatestructEventSource{voidwaitForEvent(Evente){std::coutEvent1Source;typedefEventSourceEvent2Source;structEvent12Source:publicEvent1Source,publicEvent2Source{};intmain(){Event12Sourcesource;source.waitF